|
Barton–Nackman trick is a term coined by the C++ standardization committee (ISO/IEC JTC1/SC22 WG21) to refer to an idiom introduced by John Barton and Lee Nackman as ''Restricted Template Expansion''. ==The idiom== The idiom is characterized by an in-class friend function definition appearing in the base class template component of the Curiously Recurring Template Pattern (CRTP). // A class template to express an equality comparison interface. template }; // Class value_type wants to have == and !=, so it derives from // equal_comparable with itself as argument (which is the CRTP). class value_type : private equal_comparable When a class template like equal_comparable is instantiated, the in-class friend definitions produce ''nontemplate'' (and nonmember) functions (operator functions in this case). At the time the idiom was introduced (1994) the C++ language did not define a partial ordering for overloaded function templates and as a result overloading function templates often resulted in ambiguities. For example, trying to capture a generic definition for operator== astemplate bool operator==(T const &a, T const &b) would essentially be incompatible with another definition like template bool operator==(Array The Barton–Nackman trick, then, achieves the goal of providing a generic user-defined equality operator without having to deal with such ambiguities. The adjective ''restricted'' in the idiom name refers to the fact that the provided in-class function definition is restricted (only applies) to specializations of the given class template. The term is sometimes mistakenly used to refer to the Curiously Recurring Template Pattern (CRTP). As explained above, the Barton–Nackman trick is a distinct idiom (that relies on the CRTP). 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Barton–Nackman trick」の詳細全文を読む スポンサード リンク
|